home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 2 / Mac Magazin and MacEasy Magazine CD - Issue 02.iso / Sharewarebibliothek / Applikationen / Alpha.5.81 folder / Tcl / UserCode / Text Filters / Text Munging / travesty < prev    next >
Text File  |  1994-03-14  |  1KB  |  67 lines

  1. #!/usr/bin/perl
  2.  
  3. # Usage: travesty [files]
  4.  
  5. # First analyze the input.
  6.  
  7. while (<>) {
  8.  
  9.     # Handle article headers and ">>>" quotes.
  10.  
  11.     next if /^\./;
  12.     next if /^From / .. /^$/;
  13.     next if /^Path: / .. /^$/;
  14.     s/^\W+//;
  15.  
  16.     # Do each word.
  17.  
  18.     push(@ary,split(' '));
  19.     while ($#ary > 1) {
  20.     $a = $p;
  21.     $p = $n;
  22.     $w = shift(@ary);
  23.     $n = $num{$w};
  24.     if ($n eq '') {
  25.         push(@word,$w);
  26.         $n = pack('S',$#word);
  27.         $num{$w} = $n;
  28.     }
  29.     $lookup{$a . $p} .= $n;
  30.     }
  31. }
  32.  
  33. # Now spew out the words, based on the frequencies.  If there
  34. # is more than one possibility to choose from, choose one
  35. # randomly.
  36.  
  37. for ($i=0;$i<100;$i++) {
  38.     $n = $lookup{$a . $p};
  39.     ($foo,$n) = each(lookup) if $n eq '';       # A bootstrap.
  40.     $n = substr($n,int(rand(length($n))) & 0177776,2);
  41.     $a = $p;
  42.     $p = $n;
  43.     ($w) = unpack('S',$n);
  44.     $w = $word[$w];
  45.  
  46.     # See if word fits on line.
  47.  
  48.     $col += length($w) + 1;
  49.     if ($col >= 65) {
  50.     $col = 0;
  51.     print "\n";
  52.     }
  53.     else {
  54.     print ' ';
  55.     }
  56.     print $w;
  57.  
  58.     # Paragraph every 10 sentences or so.
  59.  
  60.     if ($w =~ /\.$/) {
  61.     if (rand() < .1) {
  62.         print "\n";
  63.         $col = 80;
  64.     }
  65.     }
  66. }
  67.